﻿using Core.Extensions;
using Core.Infrastructure;
using Microsoft.AspNetCore.Builder;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;

namespace BP.Plugin.Cors;

public class BoilerplateStartup : IBoilerplateStartup
{
    public int Order => 500;

    private const string CorsPolicy = nameof(CorsPolicy);
    public void Configure(IApplicationBuilder application)
    {
        application.UseCors(CorsPolicy);
    }

    public void ConfigureServices(IServiceCollection services, IConfiguration configuration)
    {
        var config = ConfigurationsExtensions.AddJsonFile("cors");
        var corsOptions = config.GetSection(nameof(CorsOptions)).Get<CorsOptions>();

        services.AddCors(opt =>
        opt.AddPolicy(CorsPolicy, policy =>
            policy.AllowAnyHeader()
                .AllowAnyMethod()
                .AllowCredentials()
                .WithOrigins(corsOptions!.AllowedOrigins.ToArray())));
    }
}
